package com.tduck.cloud.form.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tduck.cloud.form.entity.RelationshipEntity;
import com.tduck.cloud.form.mapper.RelationshipMapper;
import com.tduck.cloud.form.service.RelationshipService;
import com.tduck.cloud.form.vo.Evaluator;
import com.tduck.cloud.form.vo.RelationshipVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 关系服务impl
 *
 * @author Guard Baby
 * @date 2023/04/20
 */
@Service
@RequiredArgsConstructor
public class RelationshipServiceImpl extends ServiceImpl<RelationshipMapper, RelationshipEntity> implements RelationshipService {
    @Override
    public List<RelationshipEntity> queryAggregateData(QueryWrapper<RelationshipEntity> queryWrapper) {
        return this.baseMapper.queryWithName( queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRelationShip(RelationshipVo relationshipVo) {
        List<RelationshipEntity> relationshipEntityList = new ArrayList<>();
        List<Evaluator> evaluators = relationshipVo.getEvaluators();
        QueryWrapper<RelationshipEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("evaluated", relationshipVo.getEvaluated());
        queryWrapper.eq("form_key", relationshipVo.getFormKey());
        this.baseMapper.delete(queryWrapper);
        if (CollUtil.isNotEmpty(evaluators)) {
            for (Evaluator evaluator : evaluators) {
                RelationshipEntity relationshipEntity = new RelationshipEntity();
                relationshipEntity.setEvaluated(relationshipVo.getEvaluated());
                relationshipEntity.setEvaluator(evaluator.getId());
                relationshipEntity.setFormKey(relationshipVo.getFormKey());
                relationshipEntity.setType(relationshipVo.getType());
                relationshipEntity.setUserRelationId(evaluator.getUserRelationId());
                relationshipEntityList.add(relationshipEntity);
            }
            return this.saveBatch(relationshipEntityList);
        }else {
            throw new RuntimeException("评价者不能为空");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addRelationShip(RelationshipVo relationshipVo) {
        QueryWrapper<RelationshipEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("evaluated",relationshipVo.getEvaluated());
        queryWrapper.eq("form_key",relationshipVo.getFormKey());
        List<RelationshipEntity> list = this.baseMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(list)){
            throw new RuntimeException("该被评价者已存在评价关系，不允许重复添加~!");
        }
        List<RelationshipEntity> relationshipEntityList = new ArrayList<>();
        if(CollUtil.isNotEmpty(relationshipVo.getEvaluators())){
            for (Evaluator evaluator : relationshipVo.getEvaluators()){
                RelationshipEntity relationshipEntity = new RelationshipEntity();
                BeanUtil.copyProperties(relationshipVo,relationshipEntity);
                relationshipEntity.setEvaluator(evaluator.getId());
                relationshipEntityList.add(relationshipEntity);
            }
            return saveBatch(relationshipEntityList);
        }
        return false;
    }
}
